package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hualan.mapper.ConsultantMapper;
import com.hualan.pojo.Consultant;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Result;
import com.hualan.service.ConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConsultantServiceImpl implements ConsultantService {
    @Autowired
    private ConsultantMapper consultantMapper;
    @Override
    public PageResult queryAllConsultant(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Consultant> consultantList = consultantMapper.queryAllConsultant(name);
        Page<Consultant> p = (Page<Consultant>) consultantList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Result addConsultant(Consultant consultant) {
       try {
           consultantMapper.addConsultant(consultant);
           return Result.success();
       } catch (Exception e) {
           e.printStackTrace();
           return Result.error("添加失败");
       }
    }

    @Override
    public Result updateConsultant(Consultant consultant) {
        try {
            consultantMapper.updateConsultant(consultant);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    @Override
    public Result deleteConsultant(List<Integer> ids) {
        try {
            consultantMapper.deleteConsultant(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @Override
    public void updateConsultantName(Integer id, String name) {
        consultantMapper.updateConsultantName(id, name);
    }

    @Override
    public Consultant searchById(Integer id) {

        return consultantMapper.searchById(id);
    }
    @Override
    public List<Consultant> find(String field, String levelName) {
        return consultantMapper.find(field,levelName);
    }

}
